package ui.everyPanel;

import cronCreationAndParse.dateProcess.GetDay;
import javax.swing.*;
import java.awt.*;

/**
 * DayPanel类
 * */
public class DayPanel extends JPanel implements PanelInterface {
    //属性
    private JPanel jp1, jp2, jp3,jp4,jp5;
    private JLabel jlb1, jlb2,jlb3,jlb4;
    private JLabel jlb31,jlb32,jlb33;
    private JLabel jlb41,jlb42,jlb43;
    private JTextField jtx31,jtx32;
    private JTextField jtx41,jtx42;
    private JCheckBox[] jcbs;
    private JRadioButton jrb1, jrb2,jrb3,jrb4,jrb5,jrb6;
    /**
     * 场景创建函数
     * */
    @Override
    public void createPanel(){
        jcbs=new JCheckBox[31];
        //创建面板
        jp1 = new JPanel();
        jp2 = new JPanel();
        jp3 = new JPanel();
        jp4 = new JPanel();
        jp5 = new JPanel();
        //创建标签
        jlb1 = new JLabel("指定：");
        jlb2 = new JLabel("单选框:");
        jlb3=new JLabel("范围:");
        jlb4=new JLabel("间隔");
        //复选框名称
        for(int i=1;i<=31;i++){
            jcbs[i-1]=new JCheckBox(""+i);
        }
        // 下面可以设置单选
        ButtonGroup bg2=new ButtonGroup();
        //单选框
        jrb1 = new JRadioButton("All");
        jrb2 = new JRadioButton("范围");
        jrb3=new JRadioButton("间隔");
        jrb4=new JRadioButton("指定");
        jrb5=new JRadioButton("不指定");
        jrb6=new JRadioButton("本月最后一天");
//        jrb6=new JRadioButton("")
        // 一定要把jrb1，jrb2放入到一个ButtonGroup里面
        ButtonGroup bg = new ButtonGroup();
        bg.add(jrb1);
        bg.add(jrb2);
        bg.add(jrb3);
        bg.add(jrb4);
        bg.add(jrb5);
        bg.add(jrb6);
        jrb1.setSelected(true);
        this.setLayout(new GridLayout(3, 1));
        jp1.add(jlb1);
        for(int i=0;i<31;++i){
            jp1.add(jcbs[i]);
        }
        jp2.add(jlb2);
        jp2.add(jrb1);
        jp2.add(jrb2);
        jp2.add(jrb3);
        jp2.add(jrb4);
        jp2.add(jrb5);
        jp2.add(jrb6);
        jp3.add(jlb3);
        jlb31=new JLabel("周期从");
        jtx31=new JTextField(3);
        jlb32=new JLabel("-");
        jtx32=new JTextField(3);
        jlb33=new JLabel("日");
        jp3.add(jlb31);
        jp3.add(jtx31);
        jp3.add(jlb32);
        jp3.add(jtx32);
        jp3.add(jlb33);
        jlb41=new JLabel("从");
        jlb42=new JLabel("日开始，每");
        jlb43=new JLabel("日执行一次");
        jtx41=new JTextField(3);
        jtx42=new JTextField(3);
        jp4.add(jlb41);
        jp4.add(jtx41);
        jp4.add(jlb42);
        jp4.add(jtx42);
        jp4.add(jlb43);
        // 加入到JFrame
        setLayout(new GridLayout(4,1));
        this.add(jp1);
        this.add(jp2);
        this.add(jp3);
        this.add(jp4);
    }
    //构造器
    /**
     * 构造器部分
     * 用于dayPanel部分的布局安排和组件放置
     * */
    public DayPanel() {

        createPanel();
    }
    /**
     * 天部分输入格式的检查
     * */
    public boolean check_day_str(DayPanel day){
        if(day.jrb5.isSelected()){
//            day_str="?";
            return true;
        }
        else if(day.jrb2.isSelected()){
            if(day.jtx31.getText().equals("") || day.jtx32.getText().equals("")){
                return false;
            }
            int tx31=Integer.parseInt(day.jtx31.getText());
            int tx32=Integer.parseInt(day.jtx32.getText());
            if(tx31<tx32 && tx31>=1 &&tx32<=31){
                return true;
            }
            return false;
        }
        else if(day.jrb3.isSelected()){
            if(day.jtx41.getText().equals("") || day.jtx42.getText().equals("")){
                return false;
            }
            int tx41=Integer.parseInt(day.jtx41.getText());
//            int tx42=Integer.parseInt(day.jtx42.getText());
            if(tx41<=0 || tx41>31){
                return false;
            }
//            day_str=day.jtx41.getText()+"\\"+day.jtx42.getText();
            return true;
        }
        else if (day.jrb4.isSelected()){
            for(int i=0;i<31;++i){
                if(day.jcbs[i].isSelected()){
                    return true;
                }
            }
            return false;
        }
        else if(day.jrb6.isSelected()){
            //本月最后一天
            return true;
        }
        else{
            //不指定的情况下
            return true;
        }

    }
    /**
     * 获取天部分输入的数据
     * */
    public String get_day_str(DayPanel day){
        String day_str="";
        if(day.jrb5.isSelected()){

            day_str="?";
        }
        else if(day.jrb2.isSelected()){
            //范围
//            System.out.println("范围");
            day_str=day.jtx31.getText()+"-"+day.jtx32.getText();
//            System.out.println(day_str);
        }
        else if(day.jrb3.isSelected()){
//            System.out.println("间隔");
            day_str=day.jtx41.getText()+"\\"+day.jtx42.getText();
//            System.out.println(day_str);
        }
        else if (day.jrb4.isSelected()){
//            System.out.println("指定");
            int is_first=0;
            for(int i=0;i<31;++i){
                if(day.jcbs[i].isSelected()){
                    if(is_first<=0){
                        is_first+=1;
                        day_str+=day.jcbs[i].getText();
                    }else{
                        day_str+=","+day.jcbs[i].getText();
                    }
                }
            }
        }
        else if(day.jrb6.isSelected()){
            //最后一天
//            Date date=new
            GetDay getDay=new GetDay();
            day_str=getDay.getLastDayOfNowMonth()+"";
        }
        else{
            //不指定的情况下
            day_str="*";
        }
        return day_str;
    }
    /**
     * 判断天是否为未指定
     * */
    public boolean getJrb5SelectedInfo(){
        if(jrb5.isSelected()){
            return true;
        }
        else{
            return false;
        }
    }
}
